const showModal = (options) => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      ...options,
      success: ({ confirm, cancel }) => {
        if (confirm) {
          resolve()
        } else {
          reject()
        }
      },
    })
  })
}

export const isLogin = async ({ modal = true } = {}) => {
  const userId = uni.getStorageSync('userId')
  if (userId) {
    return true
  }

  if (!modal) {
    throw new Error('用户未登录')
  }

  try {
    await showModal({
      title: '提示',
      content: '该功能需要进行登录，是否继续？',
      showCancel: true,
    })
    uni.navigateTo({
      url: '/subPackages/login/index',
    })
  } catch (error) {
    console.warn(error)
  }

  throw new Error('用户未登录')
}
